package com.zjy.software.day12;

import java.util.Objects;

public class AnimalTest {
    public static void main(String[] args) {
        //创建一个Animal对象，指定泛型为Dog
        Animal<Dog> dog=new Animal<Dog>();
        dog.setObj(new Dog());
        System.out.println("dog = " + dog);

        Animal<Cat> cat=new Animal<Cat>();
        cat.setObj(new Cat());
        System.out.println("cat = " + cat);
    }
}
class Dog implements Comparable<Dog>{
    private  String name;
    private int age;

    public Dog(String name) {
        this.name=name;
    }
    public Dog(String name,int age) {
        this.name=name;
        this.age=age;
    }
    Dog(){}
    public String toString(){
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Dog dog = (Dog) o;

        return Objects.equals(name, dog.name);
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

// 先按照年龄排序,如果年龄相同时,按照名字排序

    @Override
    public int compareTo(Dog o) {
        if(this.age>o.age){
            return 1;
        }else if(this.age<o.age){
            return -1;
        }else{
            return this.name.compareTo(o.name);
        }


    }
}

class Cat{
    private String name;
    private int age;
    Cat(){}
    Cat(String name,int age){
        this.setName(name);
        this.setAge(age);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Cat cat = (Cat) o;

        if (getAge() != cat.getAge()) return false;
        return getName() != null ? getName().equals(cat.getName()) : cat.getName() == null;
    }

    @Override
    public int hashCode() {
        int result = getName() != null ? getName().hashCode() : 0;
        result = 31 * result + getAge();
        return result;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}